package com.czy.form.service;

import com.czy.form.model.FormData;
import com.czy.form.model.FormInfo;
import com.czy.form.repository.FormDataRepository;
import com.czy.form.repository.FormFieldRepository;
import com.czy.form.repository.FormInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/***
 * @ ClassName: FormService
 * @ Description: TODO
 * @ author：libi
 * @ date： 2020/2/1 13:57
 * @ version： V1.0.0
 * @ CopyRight：czy
 */
@Service
@Transactional
public class FormService {

    @Autowired
    FormFieldRepository formFieldRepository;

    @Autowired
    FormInfoRepository formInfoRepository;

    @Autowired
    FormDataRepository formDataRepository;

    public List<FormInfo> listFormInfo(){
        return formInfoRepository.findAll();
    }

    public void saveFormInfo(FormInfo formInfo){
        //TODO:级联保存易产生垃圾数据，需要优化
        formInfoRepository.save(formInfo);
    }

    public FormInfo getFormInfo(String formId){
        Optional<FormInfo> optional = formInfoRepository.findById(formId);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    public void delFormInfo(String formId){
        formInfoRepository.deleteById(formId);
    }

    public List<FormData> listFormData(){
        return formDataRepository.findAll();
    }

    public void saveFormData(FormData formData){
        formDataRepository.save(formData);
    }

    public FormData getFormData(String dataId){
        Optional<FormData> optional = formDataRepository.findById(dataId);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    public void delFormData(String dataId){
        formDataRepository.deleteById(dataId);
    }
}
